package cn.liulin.java8.stream;

import cn.liulin.java8.pojo.Trader;
import cn.liulin.java8.pojo.Transaction;
import com.alibaba.fastjson.JSON;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * cn.liulin.java8.stream$
 *
 * @author ll
 * @date 2021-10-14 13:53:20
 **/
public class StreamTrader {
    public static void main(String[] args) {
        Trader raoul = new Trader("Raoul","Cambridge");
        Trader mario = new Trader("Mario","Milan");
        Trader alan = new Trader("A1an","Cambridge");
        Trader brian = new Trader("Brian","Cambridge");
        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian,2011,300),
                new Transaction(raoul,2012,1000),
                new Transaction(raoul,2011,400),
                new Transaction (mario,2012,710),
                new Transaction(mario,2012,700),
                new Transaction(alan,2012,950));
//        orderOne(transactions);
//        orderTwo();
//        orderThree(transactions);
//        orderFour(transactions);
//        orderFive(transactions);
//        orderSix(transactions);
//        orderSeven(transactions);
//        orderEight(transactions);
    }

    /**
     * （8）找到交易额最小的交易。
     * @author ll
     * @date 2021-10-14 14:19:00
     * @param transactions
     * @return void
     **/
    private static void orderEight(List<Transaction> transactions) {
        int value = transactions.stream()
                .min(Comparator.comparing(Transaction::getValue))
                .get()
                .getValue();
        System.out.println(value);
    }

    /**
     * （7）所有交易中，最高的交易额是多少？
     * @author ll
     * @date 2021-10-14 14:15:51
     * @param transactions
     * @return void
     **/
    private static void orderSeven(List<Transaction> transactions) {
        int value = transactions.stream()
                .max(Comparator.comparing(Transaction::getValue))
                .get()
                .getValue();
        System.out.println(value);
    }

    /**
     * （6）打印生活在剑桥的交易员的所有交易额。
     * @author ll
     * @date 2021-10-14 14:12:52
     * @param transactions
     * @return void
     **/
    private static void orderSix(List<Transaction> transactions) {
        transactions.stream()
                .filter(d -> d.getTrader().getCity().equals("Cambridge"))
                .map(d -> d.getValue())
                .forEach(d -> System.out.println(d));
    }

    /**
     * （5）有没有交易员是在米兰工作的？
     * @author ll
     * @date 2021-10-14 14:10:30
     * @param transactions
     * @return void
     **/
    private static void orderFive(List<Transaction> transactions) {
        boolean milan = transactions.stream()
                .anyMatch(d -> d.getTrader().getCity().equals("Milan"));
        if (milan) {
            System.out.println("又");
        }
    }

    /**
     * （4）返回所有交易员的姓名字符串，按字母顺序排序。
     * @author ll
     * @date 2021-10-14 14:05:35
     * @param transactions
     * @return void
     **/
    private static void orderFour(List<Transaction> transactions) {
        String reduce = transactions.stream()
                .map(d -> d.getTrader().getName())
                .sorted()
                .reduce("", (n1, n2) -> n1 + n2);
        System.out.println(reduce);
        String collect = transactions.stream()
                .map(d -> d.getTrader().getName())
                .sorted()
                .collect(Collectors.joining());
        System.out.println(collect);
    }

    /**
     * （3）查找所有来自于剑桥的交易员，并按姓名排序。
     * @author ll
     * @date 2021-10-14 14:02:02
     * @param transactions
     * @return void
     **/
    private static void orderThree(List<Transaction> transactions) {
        List<String> cambridge = transactions.stream()
                .filter(d -> d.getTrader().getCity().equals("Cambridge"))
                .map(x1 -> x1.getTrader().getName())
                .distinct()
                .sorted(String::compareTo)
                .collect(Collectors.toList());
        System.out.println(cambridge);
    }

    /**
     * （2） 交易员都在哪些不同的城市工作过？
     * @author ll
     * @date 2021-10-14 14:02:26
     * @param transactions
     * @return void
     **/
    private static void orderTwo(List<Transaction> transactions) {
        List<String> collect = transactions.stream()
                .map(transaction -> transaction.getTrader().getCity())
                .distinct()
                .collect(Collectors.toList());
    }

    /**
     * （1）找出2011年发生的所有交易，并按交易额排序（从低到高）
     * @author ll
     * @date 2021-10-14 13:59:40
     * @param transactions
     * @return void
     **/
    private static void orderOne(List<Transaction> transactions) {
        List<Transaction> collect = transactions.stream()
                .filter(d -> d.getYear() == 2011)
                .sorted(Comparator.comparingInt(Transaction::getValue))
                .collect(Collectors.toList());
        System.out.println(JSON.toJSONString(collect));
    }
}
